/*     */ package com.infoservice.schedule.worker;
/*     */ 
/*     */ import com.infoservice.po3.POFactory;
/*     */ import com.infoservice.po3.POFactoryBuilder;
/*     */ import com.infoservice.po3.core.context.POContext;
/*     */ import com.infoservice.schedule.ScheduleException;
/*     */ import com.infoservice.schedule.Task;
/*     */ import com.infoservice.schedule.po.pos.TaskPO;
/*     */ import com.infoservice.schedule.po.pos.TaskPlanPO;
/*     */ import com.infoservice.schedule.queue.TaskBean;
/*     */ import com.infoservice.schedule.queue.TaskQueue;
/*     */ import com.infoservice.schedule.util.TaskUtils;
/*     */ import java.io.PrintWriter;
/*     */ import java.io.StringWriter;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.apache.log4j.LogManager;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class Worker extends Thread
/*     */ {
/*  43 */   private static Logger logger = LogManager.getLogger(Worker.class);
/*  44 */   private boolean isStop = false;
/*  45 */   private int errorStackLength = 4000;
/*     */ 
/*     */   public Worker(String name)
/*     */   {
/*  52 */     super(name);
/*     */   }
/*     */ 
/*     */   public void run() {
/*  56 */     logger.info("**********************  worker name=" + getName() + " start!  *****************");
/*  57 */     while (!this.isStop) {
/*  58 */       long begin = System.currentTimeMillis();
/*  59 */       TaskBean taskBean = null;
/*     */       try {
/*  61 */         taskBean = TaskQueue.getInstance().getTaskBean();
/*     */       } catch (InterruptedException localInterruptedException) {
/*     */       }
/*  64 */       if ((taskBean != null) && (lockTask(taskBean.getLogId().longValue()))) {
/*  65 */         logger.debug("************** " + taskBean.toString() + "  ***********");
/*  66 */         logger.info("*********  lock task " + taskBean.getLogId() + " success ! *********");
/*  67 */         TaskPO log = new TaskPO();
/*  68 */         TaskPlanPO tp = null;
/*  69 */         Date endTime = null;
/*     */ 
/*  71 */         log.setRealStartTime(new Date());
/*     */         try {
/*  73 */           if (ignoreTask(taskBean)) {
/*  74 */             log.setExecuteMsg(executeTask(taskBean));
/*  75 */             log.setExecuteResult(Integer.valueOf(1));
/*  76 */             endTime = new Date();
/*  77 */             log.setRealEndTime(endTime);
/*     */ 
/*  79 */             tp = new TaskPlanPO();
/*  80 */             tp.setPlanExecuteTimes(Long.valueOf(taskBean.getPlanExecuteTimes().longValue() + 1L));
/*  81 */             tp.setPlanLastExecuteTime(endTime);
/*     */           }
/*     */         } catch (ScheduleException e) {
/*  84 */           log.setExecuteResult(Integer.valueOf(0));
/*  85 */           log.setRealEndTime(new Date());
/*     */ 
/*  87 */           log.setErrorCode(e.getErrorCode());
/*  88 */           log.setErrorMsg(e.getMessage());
/*  89 */           log.setErrorStack(getErrorStack(e, this.errorStackLength));
/*     */         } finally {
/*  91 */           if (!ignoreTask(taskBean)) {
/*  92 */             logger.info("被忽略的任务=======" + taskBean.toString());
/*  93 */             log.setRealEndTime(new Date());
/*  94 */             log.setExecuteResult(Integer.valueOf(-1));
/*  95 */             log.setMarker(Integer.valueOf(1));
/*     */           }
/*  97 */           saveTaskExeLog(taskBean.getLogId().longValue(), log, taskBean.getPlanId().longValue(), tp);
/*     */         }
/*     */       }
/* 100 */       logger.info("******************* Worker 执行花费  " + (System.currentTimeMillis() - begin) + " 毫秒  ************");
/*     */     }
/* 102 */     logger.info("*************  worker name=" + getName() + " end! ****************");
/*     */   }
/*     */ 
/*     */   private boolean ignoreTask(TaskBean taskBean) {
/* 106 */     if (taskBean.getPlanIgnoreFlag().intValue() == 0)
/* 107 */       return (TaskUtils.checkTimeRange(taskBean.getPlanStartDate(), taskBean.getPlanStart(), taskBean.getPlanEnd())) && (System.currentTimeMillis() - taskBean.getPlanStartDate().getTime() <= taskBean.getPlanStartTimeout().intValue() * 60 * 1000);
/* 108 */     if ((taskBean.getPlanIgnoreFlag().intValue() == 1) && (taskBean.getPlanRunType().intValue() == 1)) {
/* 109 */       return TaskUtils.checkTimeRange(taskBean.getPlanStartDate(), taskBean.getPlanStart(), taskBean.getPlanEnd());
/*     */     }
/* 111 */     return (taskBean.getPlanIgnoreFlag().intValue() == 1) && (taskBean.getPlanRunType().intValue() == 0);
/*     */   }
/*     */ 
/*     */   private String getErrorStack(Throwable t, int length)
/*     */   {
/*     */     try
/*     */     {
/* 120 */       StringWriter sw = new StringWriter();
/* 121 */       t.printStackTrace(new PrintWriter(sw));
/* 122 */       byte[] ret = sw.toString().getBytes("UTF-8");
/* 123 */       if (ret.length >= length) {
/* 124 */         return new String(ret, 0, length, "UTF-8");
/*     */       }
/* 126 */       return new String(ret, 0, ret.length, "UTF-8");
/*     */     }
/*     */     catch (Exception e) {
/* 129 */       logger.error("get Error Stack error! " + e.getMessage(), e);
/* 130 */     }return null;
/*     */   }
/*     */ 
/*     */   private void saveTaskExeLog(long id, TaskPO log, long tpId, TaskPlanPO tp)
/*     */   {
/*     */     try {
/* 136 */       POFactory factory = POFactoryBuilder.getInstance();
/* 137 */       TaskPO cond = new TaskPO();
/* 138 */       cond.setLogId(Long.valueOf(id));
/*     */ 
/* 140 */       factory.update(cond, log);
/* 141 */       if (tp != null) {
/* 142 */         TaskPlanPO tcond = new TaskPlanPO();
/* 143 */         tcond.setPlanId(Long.valueOf(tpId));
/*     */ 
/* 145 */         factory.update(tcond, tp);
/*     */       }
/*     */     } catch (Exception e) {
/* 148 */       logger.error("save Task execute log error!" + e.getMessage(), e);
/*     */       try
/*     */       {
/* 151 */         POContext.endTxn(true);
/*     */       } catch (Throwable t) {
/* 153 */         POContext.endTxn(false);
/* 154 */         logger.error("save Task execute log error!" + t.getMessage(), t);
/*     */       }
/*     */       try {
/* 157 */         POContext.cleanTxn();
/*     */       } catch (Throwable t) {
/* 159 */         logger.error("save Task execute log error!" + t.getMessage(), t);
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 151 */         POContext.endTxn(true);
/*     */       } catch (Throwable t) {
/* 153 */         POContext.endTxn(false);
/* 154 */         logger.error("save Task execute log error!" + t.getMessage(), t);
/*     */       }
/*     */       try {
/* 157 */         POContext.cleanTxn();
/*     */       } catch (Throwable t) {
/* 159 */         logger.error("save Task execute log error!" + t.getMessage(), t);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean lockTask(long logId) {
/*     */     try {
/* 166 */       POFactory factory = POFactoryBuilder.getInstance();
/* 167 */       TaskPO value = new TaskPO();
/* 168 */       TaskPO cond = new TaskPO();
/*     */ 
/* 170 */       cond.setLogId(Long.valueOf(logId));
/* 171 */       cond.setMarker(Integer.valueOf(0));
/* 172 */       value.setMarker(Integer.valueOf(1));
/*     */       return factory.update(cond, value) == 1;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */       while (true)
/*     */       {
/*     */         TaskPO cond;
/*     */         TaskPO value;
/*     */         POFactory factory;
/* 180 */         logger.error("Lock task error!" + e.getMessage(), e);
/*     */       }
/*     */     } finally {
/*     */       try {
/* 184 */         POContext.endTxn(true);
/*     */       } catch (Throwable t) {
/* 186 */         POContext.endTxn(false);
/* 187 */         logger.error("Lock task endTxn error!" + t.getMessage(), t);
/*     */       }
/*     */       try {
/* 190 */         POContext.cleanTxn();
/*     */       } catch (Throwable t) {
/* 192 */         logger.error("Lock task cleanTxn error!" + t.getMessage(), t);
/*     */       }
/*     */     }
/* 194 */     throw localObject;
/*     */   }
/*     */ 
/*     */   private String executeTask(TaskBean taskBean) {
/* 198 */     logger.info("start execute action " + taskBean.getActionId() + " !");
/* 199 */     Task task = null;
/*     */     try {
/* 201 */       String actionId = taskBean.getActionId();
/*     */ 
/* 203 */       Map params = new HashMap();
/* 204 */       if (taskBean.getP1Name() != null) params.put(taskBean.getP1Name(), taskBean.getP1Value());
/* 205 */       if (taskBean.getP2Name() != null) params.put(taskBean.getP2Name(), taskBean.getP2Value());
/* 206 */       if (taskBean.getP3Name() != null) params.put(taskBean.getP3Name(), taskBean.getP3Value());
/* 207 */       if (taskBean.getP4Name() != null) params.put(taskBean.getP4Name(), taskBean.getP4Value());
/* 208 */       if (taskBean.getP5Name() != null) params.put(taskBean.getP5Name(), taskBean.getP5Value());
/*     */ 
/* 210 */       Class cls = Class.forName(actionId);
/* 211 */       task = (Task)cls.newInstance();
/* 212 */       task.setParams(params);
/* 213 */       task.setPlanExcDate(taskBean.getPlanStartDate());
/* 214 */       task.setDuration(taskBean.getTaskDuration().intValue());
/* 215 */       this.errorStackLength = task.getErrorStackLenth();
/* 216 */       String ret = task.execute();
/* 217 */       if (task.isSysControlTxn()) {
/* 218 */         POContext.endTxn(task.getTxnName(), true);
/*     */       }
/* 220 */       logger.info("execute action " + taskBean.getActionId() + " success! Execute result Msg : " + ret);
/* 221 */       String str1 = ret;
/*     */       return str1;
/*     */     } catch (ScheduleException e) {
/* 223 */       if (task.isSysControlTxn()) {
/* 224 */         POContext.endTxn(task.getTxnName(), false);
/*     */       }
/* 226 */       logger.error("计划任务执行过程中出错：" + e.getMessage(), e);
/* 227 */       throw e;
/*     */     } catch (Throwable t) {
/* 229 */       if (task.isSysControlTxn()) {
/* 230 */         POContext.endTxn(task.getTxnName(), false);
/*     */       }
/* 232 */       logger.error("计划任务执行过程中出错：" + t.getMessage(), t);
/* 233 */       throw new ScheduleException(null, t.getMessage(), t);
/*     */     } finally {
/* 235 */       if (task.isSysControlTxn())
/* 236 */         POContext.cleanTxn();
/*     */       else
/* 238 */         logger.info("******************  Task control Transaction! ************************");
/*     */     }
/* 240 */     throw localObject;
/*     */   }
/*     */ 
/*     */   public void stopThread() {
/* 244 */     this.isStop = true;
/* 245 */     interrupt();
/*     */   }
/*     */ }

/* Location:           D:\Workspace\Work\ASMP_DE\lib\frame3\InfoSchedule_1.0.03.jar
 * Qualified Name:     com.infoservice.schedule.worker.Worker
 * JD-Core Version:    0.6.0
 */